<?php

namespace App\Http\Controllers\PartTime;

use App\Facades\Site;
use App\Facades\Style;
use App\Http\Controllers\Controller;
use App\Models\Common\CollArea;
use App\Models\Common\CollOption;
use App\Models\Common\WebConfig;
use App\Models\Company\CompanyBasic;
use App\Models\PartTime\PartTimeJobs;
use Illuminate\Http\Request;
use Auth;

/**
 * 兼职应聘
 * Class ApplyForController
 * @package App\Http\Controllers\PartTimePosition
 */
class AplForCtlController extends Controller
{
    /**
     * 兼职前台列表页
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        //查询类型筛选列表
        $typeList = CollOption::getOption('part_time_type');
        //查询城市信息
        $cityList = CollArea::getSiteCity();
        //查询列表
        $keyword = (!empty($request->keyword)) ? $request->keyword : '';
        $type_id = (!empty($request->type_id)) ? (int)$request->type_id : 0;
        $city_id = (!empty($request->city_id)) ? (int)$request->city_id : '';
        $salary_method = (!empty($request->salary_method)) ? $request->salary_method : 0;
        $jobLists = PartTimeJobs::whereHas('companyBasic', function ($query) use ($keyword) {
            if (strlen($keyword) > 0) {
                $query->orWhere('title', 'like', '%' . $keyword . '%')
                    ->orWhere('company_name', 'like', '%' . $keyword . '%');//关键字
            }
        })
        ->where(function ($query) use ($type_id, $city_id, $salary_method) {
            if ($type_id > 0) {
                $query->where('type_id', '=', $type_id);//类型
            }
            if (strlen($city_id) > 0) {
                $query->where('city_id', '=', $city_id);//城市
            }
            if ($salary_method > 0) {
                $query->where('salary_method', '=', $salary_method);//结算方式
            }
        })->whereIn('job_status', [1])
            ->with('companyBasic')
            ->orderBy('updated_at', 'desc')
            ->paginate(20);
        //app端信息
//        dd($jobLists);
        $app['person_ios'] = WebConfig::getConfig('appPersonHasIOS');
        $app['person_android'] = WebConfig::getConfig('appPersonHasAndroid');
        $app['company_ios'] = WebConfig::getConfig('appCompanyHasIOS');
        $app['company_android'] = WebConfig::getConfig('appCompanyHasAndroid');
        $jobLists->appends(compact('keywords', 'type_id', 'city_id', 'salary_method'))->links();//筛选信息添加到分页链接
        $filterArray = ['keyword' => $keyword, 'type_id' => $type_id, 'city_id' => $city_id, 'salary_method' => $salary_method];
        //设置标题
        $area_name = !empty($city_id) ? CollArea::getAreaName($city_id) : Site::get('area_name');
        $keyword = !empty($keyword) ? $keyword : '';
        $partTimeSearch = (strlen($keyword) > 0) ? $area_name . $keyword . '兼职招聘|' : '';
        return Style::view('part_time.index.index', compact('jobLists', 'typeList', 'cityList', 'filterArray', 'app', 'partTimeSearch'));
    }

    /**
     * 兼职前台详情页
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function show(Request $request)
    {
        //查询省市区信息
        $areaList = CollArea::getComplete();
        $id = $request->id;
        //查询信息
        $info = PartTimeJobs::where('id', '=', $id)
            ->whereIn('job_status', [1])
            ->firstOrFail();
        //浏览量增加
        $info->timestamps = false;
        $info->increment('count');
        //app端信息
        $app['person_ios'] = WebConfig::getConfig('appPersonHasIOS');
        $app['person_android'] = WebConfig::getConfig('appPersonHasAndroid');
        $app['company_ios'] = WebConfig::getConfig('appCompanyHasIOS');
        $app['company_android'] = WebConfig::getConfig('appCompanyHasAndroid');
        //查询当前所属省市区信息
        $nowArea = CollArea::getAreaAddress($info->city_id);
        $companyInfo = CompanyBasic::where('id', $info->com_id)->first();
        //公司logo
        if(!empty($companyInfo->logo)){
            $companyInfo->logo = 'upload/companylogo/' . $companyInfo->logo;
        }else{
            $companyInfo->logo = '/default/images/index/default_company_img.jpg';
        }
        //公司行业
        $companyInfo->industry = CollOption::getOptionName($companyInfo->industry, 'industry');
        //公司性质
        $companyInfo->comkind = CollOption::getOptionName($companyInfo->comkind, 'comkind');
        //公司规模
        $companyInfo->employee_num = CollOption::getOptionName($companyInfo->employee_num, 'employee_num');
        !empty($companyInfo->region) ? $companyInfo->region = CollArea::getAreaName($companyInfo->region) : $companyInfo->region = '';
        return Style::view('part_time.index.show', compact('info', 'companyInfo', 'areaList', 'nowArea', 'app'));
    }

    /**
     * 地图展示
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function trafficMap(Request $request)
    {
        $info = PartTimeJobs::where('id', '=', $request->id)->first(['longitude', 'latitude', 'coordinate_address']);
        return Style::view('part_time.index.trafficMap', compact('info'));
    }
}